package com.hanqian.kepler.core.service.core.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hanqian.kepler.common.enums.BaseEnumManager;
import com.hanqian.kepler.common.enums.DictEnum;
import com.hanqian.kepler.common.jpa.specification.Rule;
import com.hanqian.kepler.common.jpa.specification.SpecificationFactory;
import com.hanqian.kepler.core.dao.primary.core.TargetEvaluateDao;
import com.hanqian.kepler.core.entity.primary.core.Target;
import com.hanqian.kepler.core.entity.primary.core.TargetEvaluate;
import com.hanqian.kepler.core.entity.primary.core.TargetEvaluateDetail;
import com.hanqian.kepler.core.entity.primary.sys.Dict;
import com.hanqian.kepler.core.service.core.TargetEvaluateDetailService;
import com.hanqian.kepler.core.service.core.TargetEvaluateService;
import com.hanqian.kepler.core.service.flow.impl.BaseFlowServiceImpl;
import com.hanqian.kepler.core.service.sys.DictService;
import com.hanqian.kepler.core.vo.TargetEvaluateVo;
import com.hanqian.kepler.flow.base.dao.BaseFlowDao;
import com.hanqian.kepler.flow.enums.FlowEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * new file
 * ======================================
 *
 * @author dzw
 * @date 2021/1/26 16:11
 * =======================================
 */
@Service
public class TargetEvaluateServiceImpl extends BaseFlowServiceImpl<TargetEvaluate> implements TargetEvaluateService {

    @Autowired
    private TargetEvaluateDao targetEvaluateDao;
    @Autowired
    private DictService dictService;
    @Autowired
    private TargetEvaluateDetailService targetEvaluateDetailService;

    @Override
    public BaseFlowDao<TargetEvaluate> getBaseFlowDao() {
        return targetEvaluateDao;
    }

    @Override
    public boolean checkHas(String userId, String targetId) {
        if(StrUtil.isBlank(userId) || StrUtil.isBlank(targetId)) return false;
        List<Rule> rules = new ArrayList<>();
        rules.add(Rule.eq("state", BaseEnumManager.StateEnum.Enable));
        rules.add(Rule.eq("creator.id", userId));
        rules.add(Rule.eq("target.id", targetId));
        rules.add(Rule.ne("processState", FlowEnum.ProcessState.Draft));
        Long count = count(SpecificationFactory.where(rules));
        return count > 0;
    }

    @Override
    public TargetEvaluateVo vo(TargetEvaluate targetEvaluate, List<Dict> typeList) {
        List<TargetEvaluateDetail> targetEvaluateDetailList = targetEvaluateDetailService.findDetailList(targetEvaluate);
        List<TargetEvaluateVo.Detail> details = new ArrayList<>();
        Map<String, Object> map = targetEvaluateDetailList.stream().collect(Collectors.toMap(d->d.getTargetEvaluateTypeDict().getId(), Function.identity()));
        typeList.forEach(dict -> {
            TargetEvaluateDetail detail = MapUtil.get(map, dict.getId(), TargetEvaluateDetail.class);
            if(detail==null) detail = new TargetEvaluateDetail();
            details.add(new TargetEvaluateVo.Detail(detail.getId(), dict.getId(), dict.getName(), detail.getScore()));
        });

        if(targetEvaluate == null) targetEvaluate = new TargetEvaluate();
        Target target = targetEvaluate.getTarget()!=null ? targetEvaluate.getTarget() : new Target();
        String creatorName = targetEvaluate.getCreator()!=null ? targetEvaluate.getCreator().getName() : "";
        return new TargetEvaluateVo(targetEvaluate.getId(), target.getId(), target.getName(), creatorName, details);
    }

    @Override
    public TargetEvaluateVo vo(TargetEvaluate targetEvaluate) {
        return this.vo(targetEvaluate, new ArrayList<>());
    }
}
